#include <stdio.h>
#include <assert.h>
#include <Winsock2.h>

#include "Win32Socket.h"

/******lwip error no*****************/
#define  LWIP_ERR_PERM         1  /* Operation not permitted */
#define  LWIP_ERR_NOENT        2  /* No such file or directory */
#define  LWIP_ERR_SRCH         3  /* No such process */
#define  LWIP_ERR_INTR         4  /* Interrupted system call */
#define  LWIP_ERR_IO           5  /* I/O error */
#define  LWIP_ERR_NXIO         6  /* No such device or address */
#define  LWIP_ERR_2BIG         7  /* Arg list too long */
#define  LWIP_ERR_NOEXEC       8  /* Exec format error */
#define  LWIP_ERR_BADF         9  /* Bad file number */
#define  LWIP_ERR_CHILD        10  /* No child processes */
#define  LWIP_ERR_AGAIN        11  /* Try again */
#define  LWIP_ERR_NOMEM        12  /* Out of memory */
#define  LWIP_ERR_ACCES        13  /* Permission denied */
#define  LWIP_ERR_FAULT        14  /* Bad address */
#define  LWIP_ERR_NOTBLK       15  /* Block device required */
#define  LWIP_ERR_BUSY         16  /* Device or resource busy */
#define  LWIP_ERR_EXIST        17  /* File exists */
#define  LWIP_ERR_XDEV         18  /* Cross-device link */
#define  LWIP_ERR_NODEV        19  /* No such device */
#define  LWIP_ERR_NOTDIR       20  /* Not a directory */
#define  LWIP_ERR_ISDIR        21  /* Is a directory */
#define  LWIP_ERR_INVAL        22  /* Invalid argument */
#define  LWIP_ERR_NFILE        23  /* File table overflow */
#define  LWIP_ERR_MFILE        24  /* Too many open files */
#define  LWIP_ERR_NOTTY        25  /* Not a typewriter */
#define  LWIP_ERR_TXTBSY       26  /* Text file busy */
#define  LWIP_ERR_FBIG         27  /* File too large */
#define  LWIP_ERR_NOSPC        28  /* No space left on device */
#define  LWIP_ERR_SPIPE        29  /* Illegal seek */
#define  LWIP_ERR_ROFS         30  /* Read-only file system */
#define  LWIP_ERR_MLINK        31  /* Too many links */
#define  LWIP_ERR_PIPE         32  /* Broken pipe */
#define  LWIP_ERR_DOM          33  /* Math argument out of domain of func */
#define  LWIP_ERR_RANGE        34  /* Math result not representable */
#define  LWIP_ERR_DEADLK       35  /* Resource deadlock would occur */
#define  LWIP_ERR_NAMETOOLONG  36  /* File name too long */
#define  LWIP_ERR_NOLCK        37  /* No record locks available */
#define  LWIP_ERR_NOSYS        38  /* Function not implemented */
#define  LWIP_ERR_NOTEMPTY     39  /* Directory not empty */
#define  LWIP_ERR_LOOP         40  /* Too many symbolic links encountered */
#define  LWIP_ERR_WOULDBLOCK   LWIP_ERR_AGAIN  /* Operation would block */
#define  LWIP_ERR_NOMSG        42  /* No message of desired type */
#define  LWIP_ERR_IDRM         43  /* Identifier removed */
#define  LWIP_ERR_CHRNG        44  /* Channel number out of range */
#define  LWIP_ERR_L2NSYNC      45  /* Level 2 not synchronized */
#define  LWIP_ERR_L3HLT        46  /* Level 3 halted */
#define  LWIP_ERR_L3RST        47  /* Level 3 reset */
#define  LWIP_ERR_LNRNG        48  /* Link number out of range */
#define  LWIP_ERR_UNATCH       49  /* Protocol driver not attached */
#define  LWIP_ERR_NOCSI        50  /* No CSI structure available */
#define  LWIP_ERR_L2HLT        51  /* Level 2 halted */
#define  LWIP_ERR_BADE         52  /* Invalid exchange */
#define  LWIP_ERR_BADR         53  /* Invalid request descriptor */
#define  LWIP_ERR_XFULL        54  /* Exchange full */
#define  LWIP_ERR_NOANO        55  /* No anode */
#define  LWIP_ERR_BADRQC       56  /* Invalid request code */
#define  LWIP_ERR_BADSLT       57  /* Invalid slot */

#define  LWIP_ERR_DEADLOCK  LWIP_ERR_DEADLK

#define  LWIP_ERR_BFONT        59  /* Bad font file format */
#define  LWIP_ERR_NOSTR        60  /* Device not a stream */
#define  LWIP_ERR_NODATA       61  /* No data available */
#define  LWIP_ERR_TIME         62  /* Timer expired */
#define  LWIP_ERR_NOSR    	   63  /* Out of streams resources */
#define  LWIP_ERR_NONET        64  /* Machine is not on the network */
#define  LWIP_ERR_NOPKG        65  /* Package not installed */
#define  LWIP_ERR_REMOTE       66  /* Object is remote */
#define  LWIP_ERR_NOLINK       67  /* Link has been severed */
#define  LWIP_ERR_ADV          68  /* Advertise error */
#define  LWIP_ERR_SRMNT        69  /* Srmount error */
#define  LWIP_ERR_COMM         70  /* Communication error on send */
#define  LWIP_ERR_PROTO        71  /* Protocol error */
#define  LWIP_ERR_MULTIHOP     72  /* Multihop attempted */
#define  LWIP_ERR_DOTDOT       73  /* RFS specific error */
#define  LWIP_ERR_BADMSG       74  /* Not a data message */
#define  LWIP_ERR_OVERFLOW     75  /* Value too large for defined data type */
#define  LWIP_ERR_NOTUNIQ      76  /* Name not unique on network */
#define  LWIP_ERR_BADFD        77  /* File descriptor in bad state */
#define  LWIP_ERR_REMCHG       78  /* Remote address changed */
#define  LWIP_ERR_LIBACC       79  /* Can not access a needed shared library */
#define  LWIP_ERR_LIBBAD       80  /* Accessing a corrupted shared library */
#define  LWIP_ERR_LIBSCN       81  /* .lib section in a.out corrupted */
#define  LWIP_ERR_LIBMAX       82  /* Attempting to link in too many shared libraries */
#define  LWIP_ERR_LIBEXEC      83  /* Cannot exec a shared library directly */
#define  LWIP_ERR_ILSEQ        84  /* Illegal byte sequence */
#define  LWIP_ERR_RESTART      85  /* Interrupted system call should be restarted */
#define  LWIP_ERR_STRPIPE      86  /* Streams pipe error */
#define  LWIP_ERR_USERS        87  /* Too many users */
#define  LWIP_ERR_NOTSOCK      88  /* Socket operation on non-socket */

#define  LWIP_ERR_DESTADDRREQ     89  /* Destination address required */
#define  LWIP_ERR_MSGSIZE         90  /* Message too long */
#define  LWIP_ERR_PROTOTYPE       91  /* Protocol wrong type for socket */
#define  LWIP_ERR_NOPROTOOPT      92  /* Protocol not available */
#define  LWIP_ERR_PROTONOSUPPORT  93  /* Protocol not supported */
#define  LWIP_ERR_SOCKTNOSUPPORT  94  /* Socket type not supported */
#define  LWIP_ERR_OPNOTSUPP       95  /* Operation not supported on transport endpoint */
#define  LWIP_ERR_PFNOSUPPORT     96  /* Protocol family not supported */
#define  LWIP_ERR_AFNOSUPPORT     97  /* Address family not supported by protocol */
#define  LWIP_ERR_ADDRINUSE       98  /* Address already in use */
#define  LWIP_ERR_ADDRNOTAVAIL    99  /* Cannot assign requested address */
#define  LWIP_ERR_NETDOWN         100  /* Network is down */
#define  LWIP_ERR_NETUNREACH      101  /* Network is unreachable */
#define  LWIP_ERR_NETRESET        102  /* Network dropped connection because of reset */
#define  LWIP_ERR_CONNABORTED     103  /* Software caused connection abort */
#define  LWIP_ERR_CONNRESET       104  /* Connection reset by peer */
#define  LWIP_ERR_NOBUFS          105  /* No buffer space available */
#define  LWIP_ERR_ISCONN          106  /* Transport endpoint is already connected */
#define  LWIP_ERR_NOTCONN         107  /* Transport endpoint is not connected */
#define  LWIP_ERR_SHUTDOWN        108  /* Cannot send after transport endpoint shutdown */
#define  LWIP_ERR_TOOMANYREFS     109  /* Too many references: cannot splice */
#define  LWIP_ERR_TIMEDOUT        110  /* Connection timed out */
#define  LWIP_ERR_CONNREFUSED     111  /* Connection refused */
#define  LWIP_ERR_HOSTDOWN        112  /* Host is down */
#define  LWIP_ERR_HOSTUNREACH     113  /* No route to host */
#define  LWIP_ERR_ALREADY         114  /* Operation already in progress */
#define  LWIP_ERR_INPROGRESS      115  /* Operation now in progress */
#define  LWIP_ERR_STALE           116  /* Stale NFS file handle */
#define  LWIP_ERR_UCLEAN          117  /* Structure needs cleaning */
#define  LWIP_ERR_NOTNAM          118  /* Not a XENIX named type file */
#define  LWIP_ERR_NAVAIL          119  /* No XENIX semaphores available */
#define  LWIP_ERR_ISNAM           120  /* Is a named type file */
#define  LWIP_ERR_REMOTEIO        121  /* Remote I/O error */
#define  LWIP_ERR_DQUOT           122  /* Quota exceeded */

#define  LWIP_ERR_NOMEDIUM        123  /* No medium found */
#define  LWIP_ERR_MEDIUMTYPE      124  /* Wrong medium type */


#define LWIP_ERR_NSROK            0   /* DNS server returned answer with no data */
#define LWIP_ERR_NSRNODATA        160 /* DNS server returned answer with no data */
#define LWIP_ERR_NSRFORMERR       161 /* DNS server claims query was misformatted */
#define LWIP_ERR_NSRSERVFAIL      162   /* DNS server returned general failure */
#define LWIP_ERR_NSRNOTFOUND      163   /* Domain name not found */
#define LWIP_ERR_NSRNOTIMP        164 /* DNS server does not implement requested operation */
#define LWIP_ERR_NSRREFUSED       165 /* DNS server refused query */
#define LWIP_ERR_NSRBADQUERY      166   /* Misformatted DNS query */
#define LWIP_ERR_NSRBADNAME       167 /* Misformatted domain name */
#define LWIP_ERR_NSRBADFAMILY     168  /* Unsupported address family */
#define LWIP_ERR_NSRBADRESP       169 /* Misformatted DNS reply */
#define LWIP_ERR_NSRCONNREFUSED   170 /* Could not contact DNS servers */
#define LWIP_ERR_NSRTIMEOUT       171 /* Timeout while contacting DNS servers */
#define LWIP_ERR_NSROF            172 /* End of file */
#define LWIP_ERR_NSRFILE          173 /* Error reading file */
#define LWIP_ERR_NSRNOMEM         174 /* Out of memory */

#define LWIP_ERR_NSRDESTRUCTION             175 /* Application terminated lookup */
#define LWIP_ERR_NSRQUERYDOMAINTOOLONG      176 /* Domain name is too long */
#define LWIP_ERR_NSRCNAMELOOP               177 /* Domain name is too long */
/************************************/


/*
 * All Windows Sockets error constants are biased by WSAERR_BASEERR from
 * the "normal"
 */
#define WSAERR_BASEERR              10000
#define WSAERR_EINTR                (WSAERR_BASEERR+4)
#define WSAERR_EBADF                (WSAERR_BASEERR+9)
#define WSAERR_EACCES               (WSAERR_BASEERR+13)
#define WSAERR_EFAULT               (WSAERR_BASEERR+14)
#define WSAERR_EINVAL               (WSAERR_BASEERR+22)
#define WSAERR_EMFILE               (WSAERR_BASEERR+24)

/*
 * Windows Sockets definitions of regular Berkeley error constants
 */
#define WSAERR_EWOULDBLOCK          (WSAERR_BASEERR+35)
#define WSAERR_EINPROGRESS          (WSAERR_BASEERR+36)
#define WSAERR_EALREADY             (WSAERR_BASEERR+37)
#define WSAERR_ENOTSOCK             (WSAERR_BASEERR+38)
#define WSAERR_EDESTADDRREQ         (WSAERR_BASEERR+39)
#define WSAERR_EMSGSIZE             (WSAERR_BASEERR+40)
#define WSAERR_EPROTOTYPE           (WSAERR_BASEERR+41)
#define WSAERR_ENOPROTOOPT          (WSAERR_BASEERR+42)
#define WSAERR_EPROTONOSUPPORT      (WSAERR_BASEERR+43)
#define WSAERR_ESOCKTNOSUPPORT      (WSAERR_BASEERR+44)
#define WSAERR_EOPNOTSUPP           (WSAERR_BASEERR+45)
#define WSAERR_EPFNOSUPPORT         (WSAERR_BASEERR+46)
#define WSAERR_EAFNOSUPPORT         (WSAERR_BASEERR+47)
#define WSAERR_EADDRINUSE           (WSAERR_BASEERR+48)
#define WSAERR_EADDRNOTAVAIL        (WSAERR_BASEERR+49)
#define WSAERR_ENETDOWN             (WSAERR_BASEERR+50)
#define WSAERR_ENETUNREACH          (WSAERR_BASEERR+51)
#define WSAERR_ENETRESET            (WSAERR_BASEERR+52)
#define WSAERR_ECONNABORTED         (WSAERR_BASEERR+53)
#define WSAERR_ECONNRESET           (WSAERR_BASEERR+54)
#define WSAERR_ENOBUFS              (WSAERR_BASEERR+55)
#define WSAERR_EISCONN              (WSAERR_BASEERR+56)
#define WSAERR_ENOTCONN             (WSAERR_BASEERR+57)
#define WSAERR_ESHUTDOWN            (WSAERR_BASEERR+58)
#define WSAERR_ETOOMANYREFS         (WSAERR_BASEERR+59)
#define WSAERR_ETIMEDOUT            (WSAERR_BASEERR+60)
#define WSAERR_ECONNREFUSED         (WSAERR_BASEERR+61)
#define WSAERR_ELOOP                (WSAERR_BASEERR+62)
#define WSAERR_ENAMETOOLONG         (WSAERR_BASEERR+63)
#define WSAERR_EHOSTDOWN            (WSAERR_BASEERR+64)
#define WSAERR_EHOSTUNREACH         (WSAERR_BASEERR+65)
#define WSAERR_ENOTEMPTY            (WSAERR_BASEERR+66)
#define WSAERR_EPROCLIM             (WSAERR_BASEERR+67)
#define WSAERR_EUSERS               (WSAERR_BASEERR+68)
#define WSAERR_EDQUOT               (WSAERR_BASEERR+69)
#define WSAERR_ESTALE               (WSAERR_BASEERR+70)
#define WSAERR_EREMOTE              (WSAERR_BASEERR+71)

/*
 * Extended Windows Sockets error constant definitions
 */
#define WSAERR_SYSNOTREADY          (WSAERR_BASEERR+91)
#define WSAERR_VERNOTSUPPORTED      (WSAERR_BASEERR+92)
#define WSAERR_NOTINITIALISED       (WSAERR_BASEERR+93)


#define ERROR_CODE_NUMBER       30

static const int s_ErrorCodeTransform[ERROR_CODE_NUMBER][2]={
    {LWIP_ERR_IO,                    WSAERR_ETIMEDOUT},
    {LWIP_ERR_HOSTUNREACH,        WSAERR_EHOSTUNREACH},
    {LWIP_ERR_TIME,                  WSAERR_ETIMEDOUT},
    {LWIP_ERR_CONNABORTED,        WSAERR_ECONNABORTED},
    {LWIP_ERR_NOBUFS,                  WSAERR_ENOBUFS},
    {LWIP_ERR_BADF,                      WSAERR_EBADF},
    {LWIP_ERR_INVAL,                    WSAERR_EFAULT},
    {LWIP_ERR_WOULDBLOCK,          WSAERR_EWOULDBLOCK},
    {LWIP_ERR_MSGSIZE,                WSAERR_EMSGSIZE},
    {LWIP_ERR_NOPROTOOPT,          WSAERR_ENOPROTOOPT},
    {LWIP_ERR_DESTADDRREQ,        WSAERR_EDESTADDRREQ},
    {LWIP_ERR_PROTOTYPE,            WSAERR_EPROTOTYPE},
    {LWIP_ERR_PROTONOSUPPORT,  WSAERR_EPROTONOSUPPORT},
    {LWIP_ERR_SOCKTNOSUPPORT,  WSAERR_ESOCKTNOSUPPORT},
    {LWIP_ERR_OPNOTSUPP,            WSAERR_EOPNOTSUPP},
    {LWIP_ERR_PFNOSUPPORT,        WSAERR_EPFNOSUPPORT},
    {LWIP_ERR_AFNOSUPPORT,        WSAERR_EAFNOSUPPORT},
    {LWIP_ERR_ADDRINUSE,            WSAERR_EADDRINUSE},
    {LWIP_ERR_ADDRNOTAVAIL,      WSAERR_EADDRNOTAVAIL},
    {LWIP_ERR_NETDOWN,                WSAERR_ENETDOWN},
    {LWIP_ERR_NETUNREACH,          WSAERR_ENETUNREACH},
    {LWIP_ERR_NETRESET,              WSAERR_ENETRESET},
    {LWIP_ERR_CONNRESET,            WSAERR_ECONNRESET},
    {LWIP_ERR_ISCONN,                  WSAERR_EISCONN},
    {LWIP_ERR_NOTCONN,                WSAERR_ENOTCONN},
    {LWIP_ERR_SHUTDOWN,              WSAERR_ESHUTDOWN},
    {LWIP_ERR_CONNREFUSED,        WSAERR_ECONNREFUSED},
    {LWIP_ERR_HOSTDOWN,              WSAERR_EHOSTDOWN},
    {LWIP_ERR_ALREADY,                WSAERR_EALREADY},
    {LWIP_ERR_INPROGRESS,          WSAERR_EINPROGRESS}
};

static int TransformErrorCode(int errCode)
{
    for (int i = 0; i < ERROR_CODE_NUMBER; i++) {
        if (s_ErrorCodeTransform[i][1] == errCode) {
            return s_ErrorCodeTransform[i][0];
        }
    }
    assert(0 && "No ErrorCode match!!!!\n");
    return INVALID_ERROR_CODE;
}

int WIN32_FD_IS_SET(win32_socket so, win32_fd_set * set)
{
    return __WSAFDIsSet((SOCKET)so, (fd_set *)set);
}

int GetTransformErrorCode()
{
    return TransformErrorCode(GetLastError());
}

int WIN32_WSAStartup()
{
    WSADATA wsd;
    int ret = WSAStartup(0x0101, &wsd);
    if (0 != ret) {
        ret = GetLastError();
    }
    return ret;
}

int WIN32_WSACleanup()
{
    int ret = WSACleanup();
    if (0 != ret) {
        ret = GetLastError();
    }
    return ret;
}

int WIN32_socket(int af, int type, int protocal, win32_socket *pwin32_Socket)
{
    SOCKET so;

    assert(NULL != pwin32_Socket);

    so = socket(af, type, protocal);
    if (INVALID_SOCKET == so) {
        return TransformErrorCode(GetLastError());
    }

    *pwin32_Socket = (win32_socket)so;
    return 0;
}

int WIN32_connect(win32_socket s, const struct win32_sockaddr *name, int namelen)
{
    int ret = 0;

    ret = connect((SOCKET)s, (struct sockaddr *)name, namelen);
    if (0 != ret) {
        ret = TransformErrorCode(GetLastError());
    }

    return ret;
}

int WIN32_bind(win32_socket s, const struct win32_sockaddr *name, int namelen)
{
    int ret = 0;

    ret = bind((SOCKET)s, (struct sockaddr *)name, namelen);
    if (0 != ret) {
        ret = TransformErrorCode(GetLastError());
    }

    return ret;
}

int WIN32_listen(win32_socket s, int backlog)
{
    int ret = 0;

    ret = listen((SOCKET)s, backlog);
    if (0 != ret) {
        ret = TransformErrorCode(GetLastError());
    }

    return ret;
}

int WIN32_accept(win32_socket s, struct win32_sockaddr *addr, int *addrlen, win32_socket *pwin32_Socket)
{
    SOCKET so;

    assert(NULL != pwin32_Socket);

    so = accept((SOCKET)s, (struct sockaddr *)addr, addrlen);

    if (INVALID_SOCKET == so) {
        return TransformErrorCode(GetLastError());
    }

    *pwin32_Socket = (win32_socket)so;
    return 0;
}

int WIN32_send(win32_socket s, const char *buf, int len, int flags, int *outlen)
{
    int ret = 0;

    assert(NULL != outlen);

    ret = send((SOCKET)s, buf, len, flags);
    if (SOCKET_ERROR == ret) {
        ret = TransformErrorCode(GetLastError());
        return ret;
    }
    *outlen = ret;

    return 0;
}

int WIN32_recv(win32_socket s, char *buf, int len, int flags, int *recvlen)
{
    int ret = 0;

    assert(NULL != recvlen);

    ret = recv((SOCKET)s, (char FAR *)buf, len , flags);
    if (ret < 0) {
        ret = TransformErrorCode(GetLastError());
        return ret;
    }

    *recvlen = ret;
    return 0;
}

int WIN32_sendto(win32_socket s, const char *buf, int len, int flags, const struct win32_sockaddr *to, int tolen, int *sendLen)
{
    int ret = 0;

    assert(NULL != sendLen);

    ret = sendto((SOCKET)s, (const char FAR *)buf, len, flags, (const struct sockaddr FAR *)to, tolen);
    if (SOCKET_ERROR == ret) {
        ret = TransformErrorCode(GetLastError());
        return ret;
    }

    *sendLen = ret;
    return 0;
}

int WIN32_recvfrom(win32_socket s, char *buf, int len, int flags, struct win32_sockaddr *from, int *fromlen, int *recvLen)
{
    int ret = 0;

    assert(NULL != recvLen);
    assert(NULL != fromlen);

    ret = recvfrom((SOCKET)s, buf, len, flags, (struct sockaddr *)from, fromlen);
    if (SOCKET_ERROR == ret) {
        ret = TransformErrorCode(GetLastError());
        return ret;
    }

    *recvLen = ret;
    return 0;
    
}

int WIN32_getsockname(win32_socket s, struct win32_sockaddr *name, int *namelen)
{
    int ret = 0;

    assert(NULL != name);
    assert(NULL != namelen);

    ret = getsockname((SOCKET)s, (struct sockaddr *)name, namelen);
    if (0 != ret) {
        ret = TransformErrorCode(GetLastError());
    }

    return ret;
}

int WIN32_getpeername(win32_socket s, struct win32_sockaddr *name, int *namelen)
{
    int ret = 0;

    assert(NULL != name);
    assert(NULL != namelen);

    ret = getpeername((SOCKET)s, (struct sockaddr *)name, namelen);
    if (0 != ret) {
        ret = TransformErrorCode(GetLastError());
    }

    return ret;
}

int WIN32_select(int fds, win32_fd_set *readfds, win32_fd_set *writefds, win32_fd_set *exceptfds, win32_timeval *timeout, int *number)
{
    int ret = 0;

    assert(NULL != number);

    ret = select(fds, (struct fd_set *)readfds, (struct fd_set *)writefds,
                 (struct fd_set *)exceptfds, (struct timeval *)timeout);
    if (SOCKET_ERROR == ret) {
        ret = TransformErrorCode(GetLastError());
        return ret;
    }

    *number = ret;
    return 0;
}

int WIN32_ioctlsocket(win32_socket s, long cmd, unsigned long *argp)
{
    int ret = 0;

    assert(NULL != argp);

    ret = ioctlsocket((SOCKET)s, cmd, argp);
    if (SOCKET_ERROR == ret) {
        ret = TransformErrorCode(GetLastError());
    }

    return ret;
}

int WIN32_shutdown(win32_socket s, int how)
{
    int ret = 0;

    ret = shutdown((SOCKET)s, how);
    if (0 != ret) {
        ret = TransformErrorCode(GetLastError());
    }

    return ret;
}

int WIN32_closesocket(win32_socket s)
{
    int ret = closesocket((SOCKET)s);
    if (0 != ret) {
        ret = TransformErrorCode(GetLastError());
    }

    return ret;
}

int WIN32_getsockopt(win32_socket s, int level, int optname, char *optval, int *optlen)
{
    int ret = 0;

    ret = getsockopt((SOCKET)s, level, optname, optval, optlen);
    if (0 != ret) {
        ret = TransformErrorCode(GetLastError());
    }

    return ret;
}

int WIN32_setsockopt(win32_socket s, int level, int optname, const char *optval, int optlen)
{
    int ret = setsockopt((SOCKET)s, level, optname, optval, optlen);
    if (0 != ret) {
        ret = TransformErrorCode(GetLastError());
    }
    return ret;
}

unsigned int WIN32_inet_addr(const char *cp)
{
    return ((unsigned int)inet_addr(cp));
}
